<template>
	<view class="page">
		<view class="my_padding">
			<view class="checkTeam" @click="navToUrl('/pages/user/team')">查看我的团队</view>
			<view class="invite_part">
				<view class="tab-bar" style="padding: 10px auto; position: relative; top: 160px; left: 3%;">
					<uni-grid :column="3" :highlight="true" :showBorder='false'>
						<uni-grid-item v-for="(item, index) in testList" :index="index" :key="index" style="height: 80px; width: 31vw; text-align: center; ">
							<view class="grid-item-box" style="margin: 0;">
								<image v-if="item.img" :src="item.img" class="invite_img"></image>
								<text class="text">{{ item.name }}</text>
							</view>
						</uni-grid-item>
					</uni-grid>
					<view class="inviteRecode" @click="loadInvite">查看邀请收益记录</view>
				</view>
			</view>
			
			<view class="invite_regular">
				<view class="regular_tit">奖励规则</view>
				<view class="regular_contain">
					<p>1.直接邀请新用户完成注册+实名认证，您可以获得推荐奖励收益2元</p>
					<p>2.一级邀请新用户完成注册+实名认证，您可以获得推荐奖励收益1元</p>
					<p>3.二级邀请新用户完成注册+实名认证，您可以获得推荐奖励收益0.5元</p>
				</view>
			</view>
			
			<view class="invite_now" @click="navToUrl('/pages/user/recommend')">立即邀请</view>
		</view>
		
		<view v-show="isShowBox" class="profit_box">
			<view class="profit_bar">
				<view class="profit_tit">邀请收益</view><view class="close" @click="choseBox">✕</view>
			</view>
			<view class="profit_contain" @scroll="handleScroll">
				<view class="det_item" v-for="(item, index) in list" :key="index">
					<view class="det_tit">{{item.reason}}</view>
					<view class="det_time">{{item.create_time}}</view>
					<view class="det_money"><span>{{item.money}}</span>元</view>
				</view>
			</view>
			<view class="uni-loadmore" v-if="showLoadMore">{{loadMoreText}}</view>
		</view>
		<view class="cover" v-show="isShowBox"></view>
	</view>
</template>

<script>
	import tabBar from '@/components/tabBar/tabBar.vue'
	import uniPopupDialog from '@/components/uni-popup/uni-popup-dialog.vue'
	export default {
		components: {
			tabBar,
			uniPopupDialog
		},
		data() {
			return {
				user_bg: '',
				avatar: '/static/icon_avatar.png',
				userinfo: [],
				list: [],
				currentPage: '/pages/user/person',
				ifOnShow: false,
				isShowBox: false,
				unit: '',
				testList: [{
					name: '邀请好友注册',
					img: '/static/images/invite_icon01.png',
					// url: '/pages/wallet/recharge'
				}, {
					name: '注册资料认证',
					img: '/static/images/invite_icon02.png',
					//url: '/pages/wallet/cash'
				}, {
					name: '三级收益',
					img: '/static/images/invite_icon03.png',
					//url: '/pages/tasks/index'
				}],
				inviteList: [],
				pageIndex: 1,
				pageSize: 10,
				totalPages: 0,
				page: 1,
				lastPage: 0,
				loadMoreText: "加载中...",
				showLoadMore: false,
				current: 1
			};
		},
		onHide() {
			this.ifOnShow = true
		},
		onShow() {
			if (this.ifOnShow) {
				// this.getData();
			}
			let pages = getCurrentPages();
			let currPage = pages[pages.length-1];
			if (currPage.$vm.bank_id){
				this.bank_id = currPage.$vm.bank_id;
				// this.loadData();
			}
		},
		onLoad: function(option) {
			this.getData();
			// this.$refs.popupDialog.open()
		},
		onPullDownRefresh() {
			this.page = 1,
			this.lastPage = 0,
			this.list = [],
			this.loadMoreText = "加载更多",
			this.showLoadMore = false;
			
			setTimeout(() => {
				this.getData();
			}, 300);
		},			
		methods: {
			//user数据
			getData: function() {
				//获取基础参数
				// uni.showLoading({
				// 	title: "数据加载中",
				// 	mask: true
				// })
				var info = uni.getStorageSync("userinfo");
				if (info.token) {
					let timestamp = this.helper.now();
					let sign = this.helper.setSign(['username=' + info.username, 'token=' + info.token, 'timestamp=' +
						timestamp
					]);
					this.helper.req({
						api: "getInvite",
						timestamp: timestamp,
						data: {
							username: info.username,
							token: info.token
						},
						header: {
							sign: sign
						},
						method: 'GET',
					}).then(res => {
						console.log('userInfo参数:', this.userinfo)
						uni.stopPullDownRefresh();
						uni.hideLoading();
					})
				} else {
					uni.hideLoading();
				}
			},
			navToUrl: function(url) {
				return uni.navigateTo({
					url: url
				});
			},
			imageError: function(e) {
				this.avatar = '/static/icon_avatar.png';
			},
			confirmHandler() {
				// 处理确认按钮点击事件
				console.log('确认按钮点击了')
			},
			showDialog(){
				this.toggleMessage()
			},
			toggleMessage() {
				this.$refs.popupDialog.open()
			},
			dialogConfirm(done){
				done()
			},
			loadInvite(){
				this.isShowBox = true
				this.$nextTick(() => {
					const el = document.querySelector('.profit_contain');
					el.scrollTo(0,0)
				});
				this.checkInvite(0)
			},
			checkInvite(current){
				var info = uni.getStorageSync("userinfo");
				if (info.token) {
					let page = this.page;
					if(current > 0){
						let page = current;
					}
					let timestamp = this.helper.now();
					let sign = this.helper.setSign(['page='+page, 'token=' + info.token, 'timestamp=' +
						timestamp
					]);
					this.helper.req({
						api: "getInvite",
						timestamp: timestamp,
						data: {
							token: info.token,
							page:page,
						},
						header: {
							sign: sign
						},
						method: 'GET',
					}).then(res => {
					let current_page = res.data.data.list.current_page;
					let last_page = res.data.data.list.last_page;
					this.page = current_page + 1;
					this.lastPage = last_page;
					this.list = this.list.concat(res.data.data.list.data);
					if (this.page >= this.lastPage) {
						this.loadMoreText = "没有更多数据了!"
					}						
						uni.stopPullDownRefresh();
						uni.hideLoading();
					})
				} else {
					uni.hideLoading();
				}
			},
			choseBox(){
				this.isShowBox = false
				this.current = 1
			},
			handleScroll(event) {
				this.$nextTick(() => {
					const el = document.querySelector('.profit_contain');
					const offsetHeight = el.offsetHeight;
					el.onscroll = () => {
						const scrollTop = el.scrollTop;
						const scrollHeight = el.scrollHeight;
						if ((offsetHeight + scrollTop) - scrollHeight >= -1) {
							// 触达底部
							let current = this.current;
							current++;
							this.checkInvite(current)
							console.log('current:',current)
							this.current = current
						}
					};
				});
			}
		},
	};
</script>

<style lang="scss">
	page {
		font-size: 3.46667vw;
		min-width: 325px;
		color: #56595d;
		background-color: #ddeafd;
		padding-bottom: 16px;
	}
	
	.my_padding {
		background-color: #FE6109;
	}
	
	.invite_part{
		height: 200vw;
	}
	.inviteRecode{
		line-height: 30px;
		padding: 6px 0;
		height: 42px;
		width: 60%;
		position: absolute;
		bottom: -20px;
		left: 20%;
		color:#fff;
		background-color: #fea829;
		font-size: 14px;
		text-align: center;
		border-radius: 8px;
	}
	
	.invite_now{
		line-height: 30px;
		padding: 6px 0;
		height: 42px;
		width: 100%;
		position: absolute;
		bottom: 0;
		left: 0;
		color:#fff;
		background-color: #fea829;
		font-size: 14px;
		text-align: center;
	}
	
	.invite_img{
		width: 40px;
		height: 40px;
	}
	.grid-item-box text{
		line-height: 20px;
		display: inherit;
		font-size: 14px; 
		color:#333;
	}
	
	.newuser{
		min-height: 800px;
		position: relative;
	}
	
	.invite_regular{
		position: absolute;
		top: 360px;
		left:4%;
		width: 92%;
		height: 200px;
		color: #fff;
		font-size: 14px;
	}
	.regular_tit{
		font-size: 15px;
		font-weight: bold;
		line-height: 30px;
	}
	.regular_contain p{
		font-size: 14px;
	}
	
	.checkTeam{
		position: fixed;
		top: 56px;
		right: 0;
		padding: 6px 10px 6px 16px;
		line-height: 20px;
		font-size: 16px;
		background: #fea829;
		color:#fff;
		border-radius:16px 0 0 16px;
	}

	.profit_box{
		width: 80vw;
		height: 160vw;
		position: fixed;
		top: 30vw;
		left: 10vw;
		background: #fff;
		border-radius: 4vw;
		border:0.5px solid #CCC;
		z-index: 2;
		
		.profit_bar{
			width: 80vw;
			height: 12vw;
			position: relative;
			border-bottom: 0.5px solid #DDD;
			
			.profit_tit{
				position: absolute;
				top: 0;
				left: 20vw;
				width: 40vw;
				height: 12vw;
				line-height: 12vw;
				font-size: 18px;
				font-weight: bold;
				text-align: center;
			}
			.close{
				width: 10vw;
				height: 12vw;
				line-height: 12vw;
				font-size: 18px;
				font-weight: bold;
				text-align: center;
				position: absolute;
				top: 0;
				right: 0;
			}
		}
		.profit_contain{
			width: 80vw;
			height: 144vw;
			overflow-y: scroll;
			
			.det_item{
				padding: 3vw 5vw 0;
				position: relative;
				.det_tit{
					font-size: 18px;
					line-height: 30px;
				}
				.det_time{
					font-size: 14px;
					line-height: 24px;
					color:#666;
				}
				.det_money{
					width: 24vw;
					line-height: 10vw;
					text-align: center;
					font-size: 14px;
					position: absolute;
					right: 0;
					top: 8px;
					span{
						font-size: 20px;
						color: #fa8600fa;
						font-weight: bold;
					}
				}
			}
		}
	}
	.cover{
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background: #333;
		opacity: 0.7;
		z-index: 1;
	}
</style>
<!-- index -->
<style lang="scss" scoped>
	.tab-bar-footer {
		background-color: #fff;
		border-radius: 3.2vw;
		// margin: 3.33333vw auto 0;
		    margin-bottom: 60px;
			padding: 16px 0;
		width: 94vw;
		box-shadow: 0 0 25px rgba(0, 0, 0, 0.2);
	}

	.tab-bar {
		padding: 30px 0;
		background-color: #fff;
		border-radius: 3.2vw;
		// margin: 3.33333vw auto 0;
		width: 94vw;
		box-shadow: 0 0 25px rgba(0, 0, 0, 0.2);

	}
</style>

